package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.data.model.ImageUpdateModel;
import com.kbbywyclient.data.model.WorkCardImagesModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.InfoCertifyLoanContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observer;
/**
 * Description:
 * Created by FHW
 * on 2017/5/4
 */

public class InfoCertifyLoanPersenter extends BasePresenter implements InfoCertifyLoanContract.Presenter {

    private InfoCertifyLoanContract.View view;
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    //为注解了@Inject的对象注入无参构造方法构造的对象
    @Inject
    public InfoCertifyLoanPersenter(InfoCertifyLoanContract.View view) {
        this.view = view;
    }


    @Override
    public void getWorkCardImages(String SalesID) {
        rsaJsonUtil.put("SalesID", SalesID);
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.getWorkCardImagesLoan(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<WorkCardImagesModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(WorkCardImagesModel workCardImagesModel) {
                        view.loadSucWorkCard(workCardImagesModel.getData());
                    }
                });
    }

    @Override
    public void getImagesUpload(String SalesID, String SalesType, String FieldName, File upfile) {

        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("SalesType", SalesType);
        rsaJsonUtil.put("FieldName", FieldName);
//        rsaJsonUtil.put("upfile", upfile);

        //单张图片
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-mData"), upfile);
        builder.addFormDataPart("upfile", upfile.getName(), imageBody);//upfile 后台接收图片流的参数名
        List<MultipartBody.Part> parts = builder.build().parts();

        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.getImagesUpload(rsaJsonUtil.getRsaParams(), parts)).
                subscribe(new Observer<ImageUpdateModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(ImageUpdateModel imageUpdateModel) {
                        view.loadSucUpdateImg(imageUpdateModel.getData());
                    }
                });
    }
}
